package org.zn.note.designpattern;

import java.util.Date;

/**
 * 中介者模式（Mediator Pattern）
 */
public class Pattern18_Mediator {

    public static void main(String[] args) {
        User robert = new User("Robert");
        User john = new User("John");

        robert.sendMessage("Hi! John!");
        john.sendMessage("Hello! Robert!");
    }

    // 中介类
    private static class ChatRoom {
        private static void showMessage(User user, String message) {
            System.out.println(new Date().toString()
                    + " [" + user.getName() + "] : " + message);
        }
    }

    private static class User {
        private String name;

        User(String name) {
            this.name = name;
        }

        String getName() {
            return name;
        }

        void setName(String name) {
            this.name = name;
        }

        void sendMessage(String message) {
            ChatRoom.showMessage(this, message);
        }
    }
}
